\chapter{A String Class}
A simple string class was developed to keep track of the
operations on a matrix. The matrix name is private member
of a matrix, but adequate access is given for modifying it
from matrix member class functions. Implementing strings as
a class avoids some of the overhead normally associated
with character arrays in C. The allocation and length
control is encapsulated in the string member functions. The
string class is loosely associated with the string classes
in Schildt \cite{sh:c++}. 

\section{The String type: strtype}
The string\index{strtype } is declared as
\begin{verbatim}
#define MAXCHARS 80
class strtype {
   private:
       char name[MAXCHARS];
   public:
      strtype( strtype &str );
      strtype( char *str );
      strtype( void );
      strtype operator+(strtype str);
      strtype operator+(const char *str);
      strtype operator=(strtype str);
      strtype operator=(const char *str);
      void Showname( void ) { printf("%s",name) ;}
      char *StringAddress( void ) { return name; }
};
\end{verbatim} 
Strings have a maximum length that can be specified at
compile time using the symbol MAXCHARS\index{strtype!MAXCHARS}. The
entire MAXCHARS number of bytes is allocated when the
string is created.  This wastes some memory, but it avoids
some of the nasty problems associated with strings
over-writing other structures in the heap. If you need the ram,
then you can reduce MAXCHARS to a minimun of 5. This will also
speed up the program some since it will spend less time in the
string functions.

Note that the constructor function copies at most MAXCHARS
characters into the string buffer. It also terminates the
string with a null character. The default string is a null
character in position 0.

The string object can be concatenated and assigned. The
concatenation operator is '+'\index{strtype!+}. The left
operand of the plus operator must be a strtype. The plus is
overloaded to accept different kinds of right operands. The
right operands may be strings or constant character arrays.
This allows you to concatenate strings using statements
such as \verb# s1+"const"+" char"+s2 # where s1 and s2 are
strings.  The only problem is remembering that the first
term in the concatenation must be a string. (I did not see
the need of anything fancier than this in this context.)
Also, concatenation controls the length of the intermediate
results to be at most MAXCHARS.

Assignment\index{strtype!=} is overloaded for strings and
constant character arrays. Repeated assignment such as
\verb# s1=s2="a silly string"# has not been tested, but I
guess it will work, providing that the leftmost assignments only
have strings as the right operands. Again, strings are
truncated to MAXCHARS during assignment. 

The Showname member function simply prints the name. I have
used the C function printf() instead of cout throughout the
program because cout jumbled the output when piping output
to a file. (Maybe I didn't do something right for cout, but
printf worked so I kept it.)

The last member function is *StringAddress(). This merely
returns the address of the name. This function is used by
the matrix functions later. When it is used in the matrix
functions, the strtype is private, so StringAddress is also
private. This keeps you from accidently corrupting the name.

The strtype class could be replaced by more sophisticated
string functions. You could allow them to have variable
length by using pointers. You could also combine repeated
references to the pointers to the name so that duplicate
names would occupy the same space in the heap. ( See Voss
and Chui \cite{vc:disk} for more on strings.) 

\section{Example: Making a Matrix Name}

A good example of making a matrix name is found in the
routine for inverting a matrix. The section of code is 

\begin{verbatim}
     strtype newname = "invs(";
     newname = newname + ROp.Getname(ROp) + ")";
     .
     .
     .
     temp->Nameit( newname ); 
\end{verbatim}

The idea is to construct the inverse of the matrix object
ROp. The name of the matrix object is private, so it must
be accessed through the member function
Getname()\index{strtype!Getname()}. The name of the inverse should
be {\em invs(rop)} supposing the name of ROp is rop. A new
string is created using the constant string "invs(", then
'rop' is extracted from the matrix, and a parenthesis is
appended. A temporary matrix pointer was created during the
matrix inversion.  The matrix member function
Nameit()\index{strtype!Nameit()} stores the string in the matrix
name. So, two member functions of the matrix class are used
for manipulating the name of the matrix.

